package com.jerei.admin.base;

import com.jerei.plugin.api.IMenuDetailService;
import com.jerei.plugin.entity.WcmCommonMenuDetail;
import com.jerei.plugin.utils.Plugins;
import com.jerei.utils.UtilsCommon;
import com.jerei.web.utils.UtilsPluginManager;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

/**
 * Created by sunfeihu on 2016/10/26.
 */
public class MenuDetailWebRequestInterceptor implements WebRequestInterceptor {

    @Override
    public void preHandle(WebRequest webRequest) throws Exception {

    }

    @Override
    public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
        IMenuDetailService iMenuDetailService = UtilsPluginManager.getExtension(IMenuDetailService.class, Plugins.COMMON_MENU);
        Long mid = UtilsCommon.getLong(webRequest.getParameter("mid"));
        if (null != mid) {
            WcmCommonMenuDetail menuDetail = iMenuDetailService.find(mid);
            if (null != modelMap) {
                modelMap.addAttribute("menuDetail", menuDetail);
                if (null != menuDetail.getPid()) {
                    modelMap.addAttribute("parentMenuDetail", iMenuDetailService.find(menuDetail.getPid()));
                }
            }
        }
    }

    @Override
    public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {

    }
}